package org.justdroid.justdroid;

import android.content.Context;
import android.telephony.PhoneNumberUtils;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;

public class CallManager extends PhoneStateListener{
		private TelephonyManager telMgr;
		private String number, simState, serviceState;
		private final Context context;
		private ContactManager contactManager;
		
		CallManager(Context context) {
			//convert to singleton
			this.context = context;
			this.contactManager = new ContactManager(context);
			setServiceState(this.context.getResources().getString(R.string.state_in_service));
		}
		
		public void setDetails(String number, String name) {
			this.number = number;
		}
		
		public void setNumber(String number) {
			this.number = number;
		}
		
		public void setIncomingNumber(String number) {
			this.number = number;
			this.contactManager.getNameFromNumber(this.number);	
		}
		
		@Override
		public void onServiceStateChanged (ServiceState serviceState) {
			super.onServiceStateChanged(serviceState);
			String phonestate = "";
			//set the state of the service - serviceState according to the value in serviceState
			switch(serviceState.getState()) {
  			case ServiceState.STATE_EMERGENCY_ONLY:
  				if(PhoneNumberUtils.isEmergencyNumber(this.number))
  					phonestate = context.getResources().getString(R.string.state_in_service);
  				else
  					phonestate = context.getResources().getString(R.string.emergency_calls_only);
  				setServiceState(phonestate);
  				break;
  			case ServiceState.STATE_OUT_OF_SERVICE:
  				phonestate = context.getResources().getString(R.string.no_service);
  				setServiceState(phonestate);
  				break;
  			case ServiceState.STATE_POWER_OFF:
  				phonestate = context.getResources().getString(R.string.power_off);
  				setServiceState(phonestate);
  				break;
  			case ServiceState.STATE_IN_SERVICE:
  				phonestate = context.getResources().getString(R.string.state_in_service);
  				setServiceState(phonestate);
  				break;
  			default:
  				phonestate = context.getResources().getString(R.string.service_unknown_reason);
  				setServiceState(phonestate);
			}
		}
		
		public String getServiceState()
		{
			return this.serviceState;
		}
		
		public String getSimState() {			
			//get the availability of the SIM card
			telMgr = (TelephonyManager) this.context.getSystemService(Context.TELEPHONY_SERVICE);
			int simState = telMgr.getSimState();
			switch (simState) {
	                case TelephonyManager.SIM_STATE_ABSENT:
	                    this.simState = this.context.getResources().getString(R.string.sim_absent);
	                    break;
	                case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
	                	this.simState = this.context.getResources().getString(R.string.network_locked);
	                    break;
	                case TelephonyManager.SIM_STATE_PIN_REQUIRED:
	                	this.simState = this.context.getResources().getString(R.string.pin_required);
	                    break;
	                case TelephonyManager.SIM_STATE_PUK_REQUIRED:
	                	this.simState = this.context.getResources().getString(R.string.puk_required);
	                    break;
	                case TelephonyManager.SIM_STATE_READY:
	                	this.simState = this.context.getResources().getString(R.string.sim_ready);
	                    break;
	                case TelephonyManager.SIM_STATE_UNKNOWN:
	                	this.simState = this.context.getResources().getString(R.string.service_unknown_reason);
	                    break;
			}
			return this.simState;
		}
		
		public void setState(String state) {
			this.simState = state;
		}
		
		public void setServiceState(String state) {
			this.serviceState = state;
		}
	}
